import axios from 'axios';

import { createUniAppAxiosAdapter } from '@uni-helper/axios-adapter'

// 设置默认请求头
axios.defaults.headers.common['Content-Type'] = 'application/json';
// 设置超时时间（单位：毫秒）
axios.defaults.timeout = 5000;

// const baseUrl = "http://localhost:8787";
export const baseUrl = "http://192.168.124.20:8787";
const service = axios.create({
    baseURL: baseUrl,
	adapter: createUniAppAxiosAdapter()
})

// 响应拦截器，添加到已实例化的service上
service.interceptors.response.use(
    response => {
		// console.log("响应拦截器");
		if(response.data.msg === "token已过期！" && response.data.state === false){
			uni.showToast({
				title:'登录已过期，请重新登录',
				icon:'error'
			})
			return Promise.reject(response.data.msg);
		}else if(response.data.msg === 'token为空' && response.data.state === false){
			uni.showToast({
				title:'登录已过期，请重新登录',
				icon:'error'
			})
			return Promise.reject(response.data.msg);
		}else {
			 return response;
		}
    },
    error => {
		console.log(error);
		console.log("发生错误");
		if(error.name === "AxiosError"){
			 console.error('网络请求出现问题，请检查网络连接、服务器状态或网络代理/防火墙设置。');
			 uni.showToast({
			 	title: '请检查网络是否正常连接',
				icon: 'none'
			 })
		}
        return Promise.reject(error);
    }
);

export {service as axios}